<?php

namespace app\admin\controller;

use app\admin\middleware\CheckAdmin;
use app\BaseController;
use app\common\BusinessException;
use think\facade\Db;

class User extends BaseController
{

    public $middleware = [
        CheckAdmin::class,
    ];

    protected $noNeedLogin = [];

    public function index()
    {
        $id = $this->request->param('id');
        $user = Db::name('user')
            ->field('id,username,nickname,avatar,status,salary,sex,createTime,updateTime')
            ->where('id', $id)->find();
        if (!$user) {
            throw new BusinessException('user不存在');
        }
        return json(['code' => 200, 'msg' => '获取user成功', 'data' => $user]);
    }

    public function list()
    {
        $query = $this->request->param();
        $where = [];

        foreach ($query as $key => $value) {
            if ($key == 'id') {
                continue;
            } else if ($key == 'username') {
                $where[] = ['username', 'like', "%{$value}%"];
            } else if ($key == 'nickname') {
                $where[] = ['nickname', 'like', "%{$value}%"];
            } else if ($key == 'avatar') {
                $where[] = ['avatar', '=', $value];
            } else if ($key == 'salary') {
                $where[] = ['salary', '=', $value];
            } else if ($key == 'sex') {
                $where[] = ['sex', '=', $value];
            } else if ($key == 'status') {
                $where[] = ['status', '=', $value];
            }
        }

        $pageNum = $this->request->param('pageNum', 1);
        $pageSize = $this->request->param('pageSize', 10);

        $data = Db::name('user')
            ->where($where)
            ->field('id,username,nickname,avatar,salary,sex,status,createTime,updateTime')
            ->paginate([
                'list_rows' => $pageSize,
                'page' => $pageNum,
            ]);

        return json(['code' => 200, 'msg' => '获取user成功', 'data' => $data]);

    }

    public function add()
    {
        $data = $this->request->post();
        $this->validate($data, ['username' => 'require|max:50', 'password' => 'max:100', 'nickname' => 'max:50', 'avatar' => 'max:255']);
        // 校验用户名是否存在
        $user = Db::name('user')->where('username', $data['username'])->find();
        if ($user) {
            throw new BusinessException('用户名已存在');
        }
        // 校验密码
        if (strlen($data['password']) < 6) {
            throw new BusinessException('密码长度不能小于6位');
        }

        // 密码加密
        $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);

        $res = Db::name('user')->insert($data);
        if (!$res) {
            throw new BusinessException('新增user失败');
        }
        return json(['code' => 200, 'msg' => '新增user成功', 'data' => $data]);
    }

    public function edit()
    {
        $data = $this->request->post();
        $this->validate($data, ['id' => 'require', 'username' => 'require|max:50', 'nickname' => 'max:50', 'avatar' => 'max:255']);
        $res = Db::name('user')->where('id', $data['id'])->update($data);
        return json(['code' => 200, 'msg' => '编辑user成功', 'data' => $res]);
    }

    public function remove()
    {
        // 修改成软删除
        $data = $this->request->post();
        $data['deleteTime'] = time();
        $id = $this->request->param('id');
        $res = Db::name('user')->where('id', $id)->update($data);
        if (!$res) {
            throw new BusinessException('删除失败');
        }
        return json(['code' => 200, 'msg' => '删除成功', 'data' => $res]);
    }

    public function recover()
    {
        $data = $this->request->post();
        $data['deleteTime'] = 0;
        $id = $this->request->param('id');
        $res = Db::name('user')->where('id', $id)->update($data);
        if (!$res) {
            throw new BusinessException('恢复失败');
        }
        return json(['code' => 200, 'msg' => '恢复成功', 'data' => $res]);
    }

    public function changeStatus()
    {
        $id = $this->request->param('id');
        $status = $this->request->param('status');
        $res = Db::name('user')->where('id', $id)->update(['status' => $status]);
        if (!$res) {
            throw new BusinessException('修改失败');
        }
        return json(['code' => 200, 'msg' => '修改成功', 'data' => $res]);
    }

}
